import Vue from 'vue'
import VueRouter from 'vue-router'
import { MessageBox } from 'element-ui';
import LayoutView from '../views/layout/LayoutView.vue'
import HomeView from '../views/home/HomeView.vue'
import AboutView from '../views/about/AboutView.vue'
import UsersListView from '@/views/users/UsersListView.vue'
import UsersAddView from '@/views/users/UsersAddView.vue'
import SpotsListView from '@/views/spots/SpotsListView.vue'
import SpotsAddView from '@/views/spots/SpotsAddView.vue'
import TicketListView from '@/views/ticket/TicketListView.vue'
import TicketAddView from '@/views/ticket/TicketAddView.vue'
import GuideListView from '@/views/guide/GuideListView.vue'
import GuideAddView from '@/views/guide/GuideAddView.vue'
import TravelHome from '@/views/travelHome/TravelHome.vue';
import TravelHomeView from '@/views/travelHome/HomeView/HomeView.vue';
import ShowSpotsView from '@/views/travelHome/SpotsView/ShowSpotsView.vue';
import SpotsView from '@/views/travelHome/SpotsView/SpotsView.vue';
import GuideView from '@/views/travelHome/GuideView/GuideView.vue';
import ShowGuideView from '@/views/travelHome/GuideView/ShowGuideView.vue';
import UserCenterView from '@/views/travelHome/UserCenterView/UserCenterView.vue';
import TicketView from '@/views/travelHome/TicketView/TicketView.vue';
import AllWorksView from '@/views/travelHome/GuideView/AllWorksView.vue';
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
}
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch((err) => err);
}

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    name: "travel",
    component: TravelHome,
    redirect: "/travel",
    children: [
      {
        path: '/travel',
        name: 'home',
        component: TravelHomeView
      },
      {
        path: '/showspot',
        name: 'showspot',
        component: ShowSpotsView
      },
      {
        path: '/spotview',
        name: 'spotview',
        component: SpotsView
      },
      {
        path: '/ticketview',
        name: 'ticketview',
        component: TicketView
      },
      {
        path: '/guideview',
        name: 'guideview',
        component: GuideView
      }, {
        path: '/showguideview',
        name: 'showguideview',
        component: ShowGuideView
      }, {
        path: '/usercenter',
        name: 'usercenter',
        component: UserCenterView
      }, {
        path: '/allworks',
        name: 'allworks',
        component: AllWorksView
      }

    ]
  },
  {
    path: "/layout",
    name: "layout",
    component: LayoutView,
    // 路由重定向 
    redirect: "/layout/home",
    children: [
      {
        path: "/layout/home",
        name: "home",
        component: HomeView,
        meta: {
          title: "首页",
          breadcrumb: [
            {
              title: "首页",
              path: "/layout//home",
            }
          ]
        }
      },
      {
        path: "/layout/users/list",
        name: "users/list",
        component: UsersListView,
        meta: {
          title: "用户列表",
          breadcrumb: [
            {
              title: "首页",
              path: "/layout/home",
            },
            {
              title: "用户管理",
              path: "",
            },
            {
              title: "用户列表",
              path: "/layout/users/list",
            }
          ]
        }
      },
      {
        path: "/layout/users/add",
        name: "users/add",
        component: UsersAddView,
        meta: {
          title: "用户新增",
          breadcrumb: [
            {
              title: "首页",
              path: "/layout/home",
            },
            {
              title: "用户管理",
              path: "",
            },
            {
              title: "用户新增",
              path: "/layout/users/add",
            }
          ]
        }
      },
      {
        path: "/layout/spots/add",
        name: "spots/add",
        component: SpotsAddView,
        meta: {
          title: "景点新增",
          breadcrumb: [
            {
              title: "首页",
              path: "/layout/home",
            },
            {
              title: "景点管理",
              path: "",
            },
            {
              title: "景点新增",
              path: "/layout/spots/add",
            }
          ]
        }
      },
      {
        path: "/layout/spots/list",
        name: "spots/list",
        component: SpotsListView,
        meta: {
          title: "景点列表",
          breadcrumb: [
            {
              title: "首页",
              path: "/layout/home",
            },
            {
              title: "景点管理",
              path: "",
            },
            {
              title: "景点列表",
              path: "/layout/spots/list",
            }
          ]
        }
      },
      {
        path: "/layout/ticket/add",
        name: "ticket/add",
        component: TicketAddView,
        meta: {
          title: "票新增",
          breadcrumb: [
            {
              title: "首页",
              path: "/layout/home",
            },
            {
              title: "票管理",
              path: "",
            },
            {
              title: "票新增",
              path: "/layout/ticket/add",
            }
          ]
        }
      },
      {
        path: "/layout/ticket/list",
        name: "ticket/list",
        component: TicketListView,
        meta: {
          title: "票列表",
          breadcrumb: [
            {
              title: "首页",
              path: "/layout/home",
            },
            {
              title: "票管理",
              path: "",
            },
            {
              title: "票列表",
              path: "/layout/ticket/list",
            }
          ]
        }
      },
      {
        path: "/layout/guide/add",
        name: "guide/add",
        component: GuideAddView,
        meta: {
          title: "攻略新增",
          breadcrumb: [
            {
              title: "首页",
              path: "/layout/home",
            },
            {
              title: "攻略管理",
              path: "",
            },
            {
              title: "攻略新增",
              path: "/layout/guide/add",
            }
          ]
        }
      },
      {
        path: "/layout/guide/list",
        name: "guide/list",
        component: GuideListView,
        meta: {
          title: "攻略列表",
          breadcrumb: [
            {
              title: "首页",
              path: "/layout/home",
            },
            {
              title: "攻略管理",
              path: "",
            },
            {
              title: "攻略列表",
              path: "/layout/guide/list",
            }
          ]
        }
      },

      {
        path: "/layout/about",
        name: "about",
        component: AboutView,
        meta: {
          title: "关于我们"
        }
      }
    ]
  },
  {
    path: "/login",
    name: "login",
    component: () => import('@/views/login/LoginView.vue')
  },
  {
    path: "/sign",
    name: "sign",
    component: () => import('@/views/sign/SignView.vue')
  },
  {
    path: "*",
    name: "error",
    component: () => import('../views/error/ErrorView.vue')
  }
]

const router = new VueRouter({
  routes,
  mode: 'history'
})

// 路由的守卫 是对路由访问的首位 当去到不去要鉴权的页面可以直接访问 需要鉴权的页面则需要再次判断他的token
// router.beforeEach((to, from, next) => {
//   if (to.path === '/usercenter') {
//     if (localStorage?.token) {
//       next('/usercenter')
//     } else {
//       next('/login')
//     }
//   } else {
//     next();
//   }
// })

export default router
